//
//  ORMObj.m
//  storeapp
//
//  Created by huangyi on 13-4-25.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import "ORMDatabase.h"
#import "ORMObject.h"

#import <Foundation/NSKeyValueCoding.h>

#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "FMResultSet.h"

@interface ORMObject ()

@property(nonatomic,strong)NSMutableDictionary *dictionary;

@end

@implementation ORMObject

-(id)init{
    self=[super init];
    if (self) {
        self.dictionary=[NSMutableDictionary dictionary];
    }
    return self;
}

-(NSDictionary*)dump{
    if (self.rowid==InvalidRowId) {
        [self.dictionary removeObjectForKey:@"id"];
    }else{
        self.dictionary[@"id"]=@(self.rowid);
    }
    return [NSDictionary dictionaryWithDictionary:self.dictionary];
}
-(int)store{
    return [self.database store:self];
}
-(BOOL)trash{
    return [self.database trash:self];
}

- (id)objectForKeyedSubscript:(id)key{
    return [self objectForKey:key];
}
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key{
    [self setObject:obj forKey:key];
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    [self setObject:value forKey:key];
}
- (void)removeObjectForKey:(id)aKey{
    [self.dictionary removeObjectForKey:aKey];
}
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey{
    if (anObject!=nil) {
        [self.dictionary setObject:anObject forKey:aKey];
    }else{
        [self.dictionary removeObjectForKey:aKey];
    }
}
- (NSUInteger)count{
    return self.dictionary.count;
}
-(NSArray*)allKeys{
    return [self.dictionary allKeys];
}
-(NSArray*)allValues{
    return [self.dictionary allValues];
}
- (id)objectForKey:(id)aKey{
    return self.dictionary[aKey];
}
- (NSEnumerator *)keyEnumerator{
    return [self.dictionary keyEnumerator];
}

@end
